<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/14
 * Time: 17:00
 */

namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\ScoreInterface;
use App\FreeBird\Exceptions\FreeBirdException;
use App\Weixin\Contracts\UserInterface;
use App\Weixin\Models\UserScoreLogModel;
use Illuminate\Support\Facades\Request;

/***
 * 金币操作
 * Class DbScoreRepositories
 */
class  DbScoreRepositories implements ScoreInterface
{


    /**
     * 用户操作
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app()->make(UserInterface::class);
    }

    /**
     * 金币历史记录模型
     * @return UserScoreLogModel
     */
    public function getUserScoreLogModel()
    {
        return app()->make(UserScoreLogModel::class);
    }

    /**
     * 打开红包获取金币
     * @param $data
     * @return mixed|void
     */
    public function openLoginRedPack($data)
    {
        $day = get_config_param_value("LOGIN_GIFT_DAY");  //天
        $num = get_config_param_value("LOGIN_GIFT_MUM");  //次数
        $minute = get_config_param_value("LOGIN_GIFT_MINUTE"); //间隔分钟数
        $amont = get_config_param_value("LOGIN_GIFT_AMOUNT");  //奖励金额
        if (empty($amont)) {
            throw new FreeBirdException(FreeBirdException::EROOR_REDPAKE_AMOUNT_ERROR);  //无可用金额
        }
        $startDay = strtotime(date("Y-m-d", strtotime("-" . ($day - 1) . "  day")));//开始搜索时间
        $numData = $this->getUserScoreLogModel()->newQuery()->where("uid", AUID)->where("type", 1)->where("create_time", ">", $startDay)->count(); //统计数据被领了几次
        if ($num < $numData) {
            //抛出领取次数超出异常
            throw new FreeBirdException(FreeBirdException::EROOR_REDPAKE_NUM_ERROR);
        }
        //判断时间间隔
        $startTime = time() - $minute * 60;
        $isExist = $this->getUserScoreLogModel()->newQuery()->where("uid", AUID)->where("type", 1)->where("create_time", ">", $startTime)->exists();
        if ($isExist) {
            throw  new FreeBirdException(FreeBirdException::EROOR_REDPAKE_MINUTE_ERROR, ['msg' => $minute]);
        }
        return $this->getUserInterface()->updateUserScore(AUID, $amont, 1);
    }

    /**
     * 打开小程序分享联接
     * @param $data
     * @return mixed|void
     */
    public function openShareLink($data)
    {
        $uid = $data["uid"];
        $day = get_config_param_value("SHARE_GIFT_DAY");  //天
        $num = get_config_param_value("SHARE_GIFT_MUM");  //次数
        $minute = get_config_param_value("SHARE_GIFT_MINUTE"); //间隔分钟数
        $amont = get_config_param_value("SHARE_GIFT_AMOUNT");  //奖励金额
        if (empty($amont)) {
            throw new FreeBirdException(FreeBirdException::EROOR_REDPAKE_AMOUNT_ERROR);  //无可用金额
        }
        $startDay = strtotime(date("Y-m-d", strtotime("-" . ($day - 1) . "  day")));//开始搜索时间
        $numData = $this->getUserScoreLogModel()->newQuery()->where("uid", $uid)->where("type", 2)->where("create_time", ">", $startDay)->count(); //统计数据被领了几次
        if ($num <= $numData) {
            //抛出领取次数超出异常
            throw new FreeBirdException(FreeBirdException::EROOR_REDPAKE_NUM_ERROR);
        }

        //判断时间间隔
        $startTime = time() - $minute * 60;
        $isExist = $this->getUserScoreLogModel()->newQuery()->where("uid", $uid)->where("type", 2)->where("create_time", ">", $startTime)->exists();
        if ($isExist) {
            throw  new FreeBirdException(FreeBirdException::EROOR_REDPAKE_MINUTE_ERROR, ['msg' => $minute]);
        }
        if (!$this->getUserInterface()->updatePushUserGrade(AUID, $uid)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_OPTION);
        }
        return $this->getUserInterface()->updateUserScore($uid, $amont, 2);
    }

    /**
     * 获取金币列表
     * @param $data
     * @return mixed|void
     */
    public function getScoreList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'asc' : $request->input("order");
        return $this->getUserScoreLogModel()->newQuery()->where($where)->orderBy($sort,$order)->paginate($limit);
    }
}